package com.snowfeel.sfmall.security.vo;

import com.snowfeel.sfmall.core.constants.EntityConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guomenghuai
 * @date 2024/9/1
 * @Description
 */
public class SysUserDetails implements UserDetails {

    private SecurityUserVo user;

    private List<String> permissionList;

    public SysUserDetails(SecurityUserVo user, List<String> permissionList) {
        this.user = user;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = permissionList.stream()
                .filter(permission -> StringUtils.isNotEmpty(permission))
                .map(permission -> new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());
        return authorities;
    }

    public SecurityUserVo getUser() {
        return user;
    }

    public void setUser(SecurityUserVo user) {
        this.user = user;
    }

    public List<String> getPermissionList() {
        return permissionList;
    }

    public void setPermissionList(List<String> permissionList) {
        this.permissionList = permissionList;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return EntityConstants.YES.equals(user.getStatus());
    }
}
